﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EIS.ConfigViews;
using EIS.Data;
using EIS.DatabaseHelper;
using EIS.Obj;
using EIS.ViewControl.Grid;
using Janus.Windows.Ribbon;

namespace EIS.Main
{
    public partial class MainWindow : Form
    {
        public delegate void ToggleStatsBarTextDelegate(bool visable);
        public ToggleStatsBarTextDelegate ToggleStatsBarText;

        public MainWindow()
        {
            InitializeComponent();
            navBarPanelContainer.Controls.Add(new NavBarControl(this.uiPanelManagerMain));
            navBarPanelContainer.Controls[0].Dock = DockStyle.Fill;
            ToggleStatsBarText = new ToggleStatsBarTextDelegate(SetStatusBarMethod);
            
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            RoutineJobScheduler.ClientRoutineBatchStarter(this);
        }

        private void navBarPanelContainer_Click(object sender, EventArgs e)
        {

        }

        public void SetItemView(UserControl uc)
        {
            this.ItemViewPanelContainer.Controls.Clear();
            this.ItemViewPanelContainer.Controls.Add(uc);
            this.ItemViewPanelContainer.Controls[0].Dock = DockStyle.Fill;

        }


        private void ribbonMain_CommandClick(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
        {
            switch (e.Command.Key)
            {
                case "MaintainSAPConnection":
                    new MaintainSapConnection().ShowDialog();
                        
                    break;
                case "TestHarness":
                    TestHarness();
                    break;
                case "MaintainLayout":
                    new MaintainLayout().ShowDialog(this);

                    break;
                case "DefineFunctionLayout":
                    //Only ViewGridDisplay Has this button enabled. 
                    DialogResult dialogResult = new DialogResult();
                    
                    if (!string.IsNullOrEmpty(GlobalVariable.Instance.CurrentFunctionID))
                    {
                        dialogResult = new FunctionViewLayout(GlobalVariable.Instance.CurrentFunctionID).ShowDialog();
                    }
           
                    if(( GlobalVariable.Instance.CurrentViewHandler != null) && (dialogResult != DialogResult.Cancel))
                         ((ViewGridDisplay)(GlobalVariable.Instance.CurrentViewHandler)).ResetLayout();
                   

                    break;
                case "ForceRefreshFromServer":

                    BackgroundWorker bgwRefreshFromServer = new BackgroundWorker();
                    bgwRefreshFromServer.DoWork +=new DoWorkEventHandler(bgwRefreshFromServer_DoWork);
                    bgwRefreshFromServer.RunWorkerAsync();
                    break;
                case "Config":
                    new GeneralConfig().ShowDialog();
  
                    break;
                case "OpenInSourceSystem":
                    if (GlobalVariable.Instance.CurrentViewHandler != null)
                        ((ViewGridDisplay)(GlobalVariable.Instance.CurrentViewHandler)).OpenRow();
  
                    break;
                 
                default:
                    break;
            }

            if (e.Command.Key.StartsWith("dyn") )
            {
                var layout = e.Command.Key;
                layout = layout.Substring(6);

                if (GlobalVariable.Instance.CurrentViewHandler != null)
                {
                    ((ViewGridDisplay) (GlobalVariable.Instance.CurrentViewHandler)).SetLayout(layout);
                }
                
            }

        }

        private void bgwRefreshFromServer_DoWork(object sender, DoWorkEventArgs e)
        {
            RoutineJobScheduler.ForceRefreshFromServer();
        }

        private void editBoxSearch_TextChanged(object sender, EventArgs e)
        {

        }

        public void SetButtonStatus(string buttonName, bool enabled)
        {
            var button = this.ribbonMain.GetAllCommands()[buttonName];
            if ( button != null )
            {
                button.Enabled = enabled;
            }
        }

        public void SetNotifyIcon(String text)
        {
            this.notifyIcon.ShowBalloonTip(20000, "QuickActioner Notification", text,
                                           System.Windows.Forms.ToolTipIcon.Info);
        }

        public void TestHarness()
        {


            
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            if (this.WindowState == FormWindowState.Minimized)
            this.WindowState = FormWindowState.Maximized;
            this.ShowInTaskbar = true;
            this.Activate();
        }

        private void MainWindow_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
               
            }
            else
            {
               
            }
        }

        public void SetStatusBarMethod(bool visible)
        {
            
            this.ribbonStatusBar1.GetAllCommands()["StatusLabel"].Visible = visible;
        }

        public void SetStatusBar(bool visible)
        {
            BeginInvoke(this.ToggleStatsBarText, visible);
        }

        private void ddLayout_DropDownOpening(object sender, CommandCancelEventArgs e)
        {
            //GlobalVariable.Instance.CurrentFunctionID
            List<string> layouts = ClientUtility.ClientConfig.Instance.GetMaintainedLayout();
            DropDownCommand command;
            this.ddLayout.Commands.Clear();
            foreach (var layout in layouts)
            {
                command = new Janus.Windows.Ribbon.DropDownCommand();
                command.Text = layout;
                command.Key = "dynCmd" + layout;
                command.Name = "dynCmd" + layout;

                this.ddLayout.Commands.AddRange(new Janus.Windows.Ribbon.CommandBase[] {
            command});
            }


            if (!string.IsNullOrEmpty(GlobalVariable.Instance.CurrentFunctionID))
            {
                command = new Janus.Windows.Ribbon.DropDownCommand();
                command.Text = "Maintain Layouts";
                command.Key = "MaintainLayout";
                command.Name = "Maintain Layouts";
                this.ddLayout.Commands.AddRange(new Janus.Windows.Ribbon.CommandBase[]
                                                    {
                                                        command
                                                    });
            }

        }

        void ribbonGroup1_Click(object sender, CommandEventArgs e)
        {
        //    throw new NotImplementedException();
        }


    }
}
